package com.demo.jsf;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import javax.faces.context.FacesContext;

import com.demo.javabean.DBAccess;

public class NoticeAction extends BaseAction {

	private String id;
	private String sender;
	private String title;
	private String content;
	private String sendtime;

	public String list() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 取得分页参数
		String strPageSize = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageSize");
		if (strPageSize != null) {
			pageSize = Integer.parseInt(strPageSize);
		}
		String strPageNo = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageNo");
		if (strPageNo != null) {
			pageNo = Integer.parseInt(strPageNo);
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 查询总的记录数，计算跳页参数
			int start = pageSize * (pageNo - 1);
			// 总记录数查询SQL
			String sql1 = "select count(*) as countall from address";
			db.query(sql1);
			if (db.next()) {
				// 计算总行数并保存
				String countall = db.getValue("countall");
				rowCount = Integer.parseInt(countall);

				// 计算总页数并保存
				pageCount = rowCount % pageSize == 0 ? rowCount / pageSize
						: rowCount / pageSize + 1;

				// 计算跳页参数并保存
				pageFirstNo = 1;// 首页
				pageLastNo = pageCount;// 尾页
				pagePreNo = pageNo > 1 ? pageNo - 1 : 1;// 前一页
				pageNextNo = pageNo < pageCount ? pageNo + 1 : pageCount;// 后一页
			}
			db.closeRs();

			// 取得当前页数据SQL
			String sql2 = "select * from notice order by sendtime desc limit "
					+ start + "," + pageSize;
			list = new ArrayList<Hashtable<String, String>>();
			db.query(sql2);
			while (db.next()) {
				// 查询每行数据的各个字段数据
				Hashtable<String, String> hash = new Hashtable<String, String>();
				hash.put("id", db.getValue("id"));
				hash.put("sender", db.getValue("sender"));
				hash.put("title", db.getValue("title"));
				hash.put("content", db.getValue("content"));
				hash.put("sendtime", db.getValue("sendtime"));

				// 保存当前行
				list.add(hash);
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}

		return "noticelist";
	}

	public String add() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		return "noticeadd";
	}

	public String insert() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			sendtime = format.format(new Date());

			// 组合新增SQL
			String sql = "insert into notice (sender, title, content, sendtime) ";
			sql += " values('" + loginAction.getUsername() + "','" + title
					+ "','" + content + "','" + sendtime + "')";

			// 转换参数编码
			try {
				sql = new String(sql.getBytes("ISO8859-1"), "UTF-8");
			} catch (Exception e) {
			}

			// 执行插入
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String edit() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "noticelist";
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 根据id编号查询当前行记录
			String sql = "select * from notice where id=" + id;
			db.query(sql);
			if (db.next()) {
				// 取得各个字段的值并保存
				sender = db.getValue("sender");
				title = db.getValue("title");
				content = db.getValue("content");
				sendtime = db.getValue("sendtime");
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		return "noticeedit";
	}

	public String update() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 组合更新SQL
			String sql = "update notice set title='" + title + "', content='"
					+ content + "' where id=" + id;

			// 转换参数编码
			try {
				sql = new String(sql.getBytes("ISO8859-1"), "UTF-8");
			} catch (Exception e) {
			}

			// 执行更新
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String delete() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "noticelist";
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 根据id组成删除SQL，执行删除
			String sql = "delete from notice where id=" + id;
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getSendtime() {
		return sendtime;
	}

	public void setSendtime(String sendtime) {
		this.sendtime = sendtime;
	}
}